package gtug.duesseldorf.client.ui;

import gtug.duesseldorf.client.model.ClientLoginData;
import gtug.duesseldorf.client.res.Constants;
import gtug.duesseldorf.client.util.StringUtils;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;

public class LoginPanel extends Composite {

	private final TextBox m_tfServer;
	private final TextBox m_tfUser;

	public LoginPanel() {
		FlexTable t = new FlexTable();
		initWidget(t);

		t.setWidget(0, 0, new HTML("Server: "));
		m_tfServer = new TextBox();
		t.setWidget(0, 1, m_tfServer);

		t.setWidget(1, 0, new HTML("Username: "));
		m_tfUser = new TextBox();
		t.setWidget(1, 1, m_tfUser);

		m_tfServer.setText(Constants.DEFAULT_SERVER);
		m_tfUser.setText(Constants.DEFAULT_USER);
	}

	public ClientLoginData getLoginData() {

		String server = StringUtils.trimToNull(m_tfServer.getText());
		String user = StringUtils.trimToNull(m_tfUser.getText());

		if (server == null || user == null) {
			return null;
		} else {
			return new ClientLoginData(server, user);
		}
	}

	public void setFocus() {
		m_tfServer.setFocus(true);
	}
}
